/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table.cell.view;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.plaf.BorderUIResource;
import javax.swing.table.TableCellRenderer;
import org.rational.sheet.table.RationalTable;
import org.rational.sheet.table.RationalTableData;
import org.rational.sheet.table.cell.model.Cell;
import org.rational.sheet.table.cell.model.CellAddress;
import org.rational.sheet.table.cell.model.setting.CellView;

/**
 *
 * @author Overmind
 */
public class RationalTableCellRenderer
    implements TableCellRenderer
{
    private JLabel label;
    private Border defaultBorder;
    private Border selectedBorder;
    
    static int count = 0;
    
    {
        label = new JLabel();
	defaultBorder = label.getBorder();
	selectedBorder = new BorderUIResource.LineBorderUIResource(Color.black);
    }
    
    public RationalTableCellRenderer()
    {
        
    }
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
        boolean isSelected, boolean hasFocus, int row, int column) 
    {
	
	RationalTable rationalTable = (RationalTable)table;
	RationalTableData tableData = rationalTable.getTableData();
	
	Cell cell = tableData.getCellAt(new CellAddress(row, column));
        CellView view = cell.getView();
	
	label.setText(cell.getFormatedValue());
	label.setFont(view.getFont());
	
	label.setForeground(view.getTextColor());
	label.setOpaque(true);
	
	if(rationalTable.isOneCellSelected() && isSelected)
	{
	    label.setBorder(selectedBorder);
	    label.setBackground(view.getBackgroundColor());
	}
	else
	{
	    label.setBorder(defaultBorder);
	    
	    if(isSelected)
	    {
		label.setBackground(Color.blue);
	    }
	    else
	    {
		label.setBackground(view.getBackgroundColor());
	    }
	}
	
	label.setHorizontalAlignment(view.getHorizontalAlignment().getCode());
	label.setVerticalAlignment(view.getVerticalAlignment().getCode());
        
        return label;
    }
    
}
